<?php

declare(strict_types=1);

namespace app\shop\controller\api\user;

use app\shop\controller\api\ShopBase;
use think\Request;
use sheep\facade\Sms;
use sheep\model\user\User as UserModel;
use app\shop\model\user\Coupon as UserCouponModel;
use app\shop\model\order\Order as OrderModel;
use app\shop\model\order\Aftersale as AftersaleModel;
use sheep\model\ThirdOauth;

class User extends ShopBase
{
    /**
     * 用户数据
     */
    public function data()
    {
        $user = $this->auth()->user();
        // 查询用户优惠券数量
        $data['coupons_num'] = UserCouponModel::geted()->where('user_id', $user->id)->count();

        // 订单数量
        $orderNum = [];
        $orderNum['unpaid'] = OrderModel::where('user_id', $user->id)->unpaid()->count();
        $orderNum['nosend'] = OrderModel::where('user_id', $user->id)->pretendPaid()->nosend()->count();
        $orderNum['noget'] = OrderModel::where('user_id', $user->id)->pretendPaid()->noget()->count();
        $orderNum['nocomment'] = OrderModel::where('user_id', $user->id)->paid()->nocomment()->count();
        $orderNum['aftersale'] = AftersaleModel::where('user_id', $user->id)->needOper()->count();

        $data['order_num'] = $orderNum;

        return success('用户数据', $data);
    }



    // 第三方授权信息
    public function thirdOauth(Request $request)
    {
        $user = $this->auth()->user(true);

        $provider = $request->param('provider', '');
        $platform = $request->param('platform', '');
        if(!in_array($platform, ['miniProgram', 'officialAccount', 'openPlatform'])) {
            return error('参数有误');
        }
        $where = [
            'platform' => $platform,
            'user_id' => $user->id
        ];
        if($provider !== '') {
            $where['provider'] = $provider;
        }
        $oauth = ThirdOauth::where($where)->field('nickname, avatar, platform, provider')->find();
        return success('', $oauth);
    }
    
}
